﻿using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using LuckyCoding.Models;
using WebViewControl;

namespace LuckyCoding.FileView;

public partial class WebFileView : UserControl
{
    public WebFileView()
    {
        InitializeComponent();
        UrlBox = this.FindControl<TextBox>("UrlBox");
        View = this.FindControl<WebView>("View");
    }
    
    public WebFileView(string? url = null)
    {
        InitializeComponent();
        UrlBox = this.FindControl<TextBox>("UrlBox");
        View = this.FindControl<WebView>("View");
        if (string.IsNullOrEmpty(url)) return;
        UrlBox.Text = url;
        View.Address = url;
    }

    public WebFileView(WebFileModel model)
    {
        InitializeComponent();
        UrlBox = this.FindControl<TextBox>("UrlBox");
        View = this.FindControl<WebView>("View");
        UrlBox.Text = model.Location;
        View.Address = model.Location;
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }

    private void UrlBoxOnKeyDown(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
            View.Address = UrlBox.Text;
    }
}